package com.itbbs.db.dataSource;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 动态切换数据源
 *
 * @author y
 * @date 2021-06-01-16:52
 */
public class DBContextHolder {

    // 确保创建的变量只被通一个线程操作
    private static final ThreadLocal<DBTypeEnum> THREAD_LOCAL = new ThreadLocal();

    private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();

    // 设置当前线程的设置当前线程的
    public static void set(DBTypeEnum dbType){
        THREAD_LOCAL.set(dbType);
    }

    // 获取当前线程的枚举值
    public static DBTypeEnum get(){
        return THREAD_LOCAL.get();
    }

    // 移除当前线程的枚举值
    public static void remove(){
        THREAD_LOCAL.remove();
    }

    // 切换到主库
    public static void master(){
        set(DBTypeEnum.MASTER);
    }

    // 切换到从库
    public static void slave(){
        // 目前只有一个从从库
        set(DBTypeEnum.SLAVE);
    }

}
